<template>
  <div>
    <eagle-dialog-page
      @submit="submit"
      @callbackDialog="callback"
      @setData="setData"
      @initList="_initList"
      @open="open"
      :url="url"
      :params="queryParams"
      v-bind="$attrs"
      v-on="$listeners"
      :option="option"
      ref="eagleDialogPage"
    >
      <eagle-cell
        v-for="(item, index) in list"
        :key="index"
      >
        <div class="user-item">
          <div>
            <div class="mb-5 color-gray1">{{ item.CompanyName }}</div>
          </div>
          <eagle-checkbox
            v-if="$attrs.isMulti?$attrs.isMulti:false"
            :name="item.CompanyCode"
            shape="round"
          />
          <eagle-radio
            v-else
            :name="item.CompanyCode"
          ></eagle-radio>
        </div>
      </eagle-cell>
    </eagle-dialog-page>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: "选择下级单位",
    },
    selectedValue: {
      type: String,
      default: () => "",
    },
    tagData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      option: { name: "CompanyName", code: "CompanyCode" },
      queryParams: [{
        dataType: "orgCompanyList",
        accounted: true,
        userKind: 0,
      }],
      url: "/UserCompany/GetPageData",
      controller: "/UserCompany",
      conditions: {},
      list: [],
      isMulti: false,
      simSelectObject: { code: "", name: "" },
      selected: [],
      isDefault:true,
      showSelectUser: false,
    };
  },
  computed: {
    multi: {
      get() {
        if (this.$attrs.isMulti) {
          return this.$attrs.isMulti;
        } else {
          return false;
        }
      },
    },
  },
  methods: {
    _initList(list) {
      this.selected = [];
      if(this.isDefault){
        this.selected = this.tagData;
      }
      this.list = list;
      let newData = [];
      if(!this.multi){
        newData = [{ code: this.selectedValue, CompanyCode: this.selectedValue }];
        this.$refs.eagleDialogPage.initSeletedData(newData);
        this.selected = newData;
      }else{
        this.$refs.eagleDialogPage.initSeletedData(this.selected);
      }
    },
    open(list) {
      this.list = list;
    },
    callback(data) {
      if (this.multi) {
        this.$emit("callback", this.selected);
      } else {
        this.$emit("callback", data);
      }
    },
    setData(data){
      
      if (this.multi) {
        this.selected = data;
      } else {
        this.simSelectObject = data;
      }
    },
    submit() {
      this.$refs.eagleDialogPage.submit();
    },
  },
};
</script>
<style scoped>
.user-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>